﻿<MCard Width="500" Style="margin: 0 auto">
    <MVirtualScroll Height="250" Items="_items" @ref="_vs">
        <ItemContent>
            <div>
                <MListItem>
                    <MListItemContent>
                        <MListItemTitle>@context</MListItemTitle>
                    </MListItemContent>
                </MListItem>
                <MDivider />
            </div>
        </ItemContent>
        <FooterContent>
            <MInfiniteScroll HasMore="@_hasMore"
                             OnLoadMore="@LoadMore"
                             Parent="@_vs?.Ref">
            </MInfiniteScroll>
        </FooterContent>
    </MVirtualScroll>
</MCard>


@code {

    private MVirtualScroll<string> _vs;

    private int _count;
    private List<string> _items = new();
    private bool _hasMore;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await LoadMore();
            StateHasChanged();
        }
    }

    private async Task LoadMore()
    {
        var append = await MockRequest();
        _items.AddRange(append);
        _hasMore = append.Count > 0;
    }

    private async Task<List<string>> MockRequest()
    {
        if (_count >= 3)
        {
            return new List<string>();
        }

        await Task.Delay(2000);

        _count++;

        return Enumerable.Range(_items.Count, 10).Select(n => n.ToString()).ToList();
    }

}
